import 'package:flutter/material.dart';
import 'package:hxdj/common/base_page/my_loading_page.dart';
import 'package:hxdj/common/components/my_expansion.dart';
import 'package:hxdj/common/my_header_global.dart';
import 'package:hxdj/mine/questions/questions_viewmodel.dart';

// 常见问题
class QuestionsPage extends StatefulWidget {
  static String routeName = "/QuestionsPage";

  const QuestionsPage({super.key});

  @override
  State<QuestionsPage> createState() => _QuestionsPageState();
}

class _QuestionsPageState extends State<QuestionsPage> implements OnStatusChangeListener {
  //viewModel
  QuestionsViewModel? viewmodel;

  //是否折叠
  @override
  void initState() {
    super.initState();
  }

  @override
  void onStatusChange(int widgetId) {
    FaqModel faq = viewmodel!.dataList[widgetId];
    setState(() {
      faq.isExpanded = !faq.isExpanded;
    });
  }

  _buildBody() {
    switch (viewmodel!.state) {
      case ViewState.loading:
        return const MyLoadingWidget();

      default:
        return ListView.builder(
            itemCount: viewmodel!.dataList.length,
            itemBuilder: (context, index) {

              FaqModel faq = viewmodel!.dataList[index];
              
              return Container(
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(6.5),
                ),
                margin: EdgeInsets.symmetric(vertical: 7.5.w, horizontal: 10.w),
                child: Column(
                  children: [
                    MyExpansionLayout(
                      onClick: this,
                      isCanExpanded: true,
                      widgetId: index,
                      trailing: Container(
                          alignment: Alignment.centerLeft,
                          padding: EdgeInsets.fromLTRB(8.5.w, 18.w, 15.w, 18.w),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Text(faq.title ?? ''),
                              faq.isExpanded
                                  ? const MyImage(imageName: "ic_arrow_right_reversal", width: 13, height: 7.5)
                                  : const MyImage(imageName: "ic_arrow_right", width: 7.5, height: 13)
                            ],
                          )),
                      isExpanded: faq.isExpanded,
                      children: [
                        Container(
                          margin: const EdgeInsets.fromLTRB(13, 0, 13, 13),
                          decoration: BoxDecoration(
                            color: MyColours.colorF6F6F6,
                            borderRadius: BorderRadius.circular(6.5),
                          ),
                          padding: const EdgeInsets.symmetric(horizontal: 13, vertical: 8),
                          alignment: Alignment.centerLeft,
                          child: Text(faq.desc ?? ""),
                        )
                      ],
                    ),
                  ],
                ),
              );
            });
    }
  }

  @override
  Widget build(BuildContext context) {
    return BasePage<QuestionsViewModel>(
      onModelReady: (model) {
        // 页面初始化完成后的回调
        viewmodel = model;
        model.requestData();
      },
      viewModel: QuestionsViewModel(),
      builder: (BuildContext context, QuestionsViewModel? model, Widget? child) {
        // cancelAccountViewModel = model;
        return Scaffold(
            backgroundColor: const Color(0xFFF5F5F5), appBar: const MyAppBar(title: '常见问题'), body: _buildBody());
      },
    );
  }
}

